//
//  ScrumView.swift
//  codelabs
//
//  Created by 雷雷 on 25/05/2025.
//

import SwiftUI
import SwiftData

struct ScrumView: View {
    @Environment(\.modelContext) private var context
    @Query(sort:\DailyScrum.title) private var scrums:[DailyScrum]
    @State private var isEditing=false
    
    
    var body: some View {
        NavigationStack{
                if scrums.isEmpty {
                    EmptyView(message:"No scrum")
                        .navigationTitle("Daily Scrum")
                }else{
                    List{
                        ForEach(scrums){data in
                            NavigationLink(destination: ScrumDetailView(scrum:data)){
                                CardView(scrum:data)
                                    .environment(data)
                            }
                            .listRowBackground(data.theme.mainColor)
                        }
                        .onDelete{indices in
                            indices.forEach{ index in
                                context.delete(scrums[index])
                            }
                        }
                        
                    }
                    .listRowSpacing(16)
            }
            
        }
        .navigationTitle("Daily Scrum")
        .toolbar{
            Button(action:{
                isEditing=true
            }){
                Image(systemName: "plus")
                    .accessibilityLabel("New scrum")
            }
        }
        .sheet(isPresented:$isEditing){
            NavigationStack{
                ScrumEditor()
                    .navigationTitle("New Scrum")
            }
        }
        
    }
    
    
}

#Preview(traits: .dailyScrumSampleData){
    ScrumView()
}
